import * as dd from "dingtalk-jsapi";
import {
    dingLogin
} from "@/api/login";
import {
    CROP_ID,
    appId
} from '@/utils/utils.ts'
import {
    message
} from 'ant-design-vue'

export default {
    data() {
        return {
            loginLoading: false,
            freeLoginTip: ''
        }
    },
    methods: {
        async ready(func, ...args) {
            this.loginLoading = true
            this.freeLoginTip = '正在连接钉钉...'

            /*  */
            if (dd.env.platform == "notInDingTalk") {
                // message.error('请在钉钉内打开此应用')
                message.warning("本地测试");
                this.pageTextInit()
                this.loginLoading = false
                return false
            }
            await dd.ready(async function () {
                await func.apply(this, args)
            })
        },
         async getUerCode(self) {
            let code;
                await dd.runtime.permission.requestAuthCode({
                    corpId: CROP_ID, // 企业id
                    onSuccess: function (info) {
                        code = info.code // 通过该免登授权码可以获取用户身份
                    },
                    onFail: function (err) {
                        code = ''
                        message.warning(err.errorMessage)
                        setTimeout(() => {
                            self.loginLoading = false
                            self.freeLoginTip = ''
                        }, 1000);
                    },
                });
            return code
        },
        async dingLogin(auth) {
            return await dingLogin({
                auth,
                appId
            })
        },
        async freeLogin() {
            message.success('登录中,请稍等...')
            const {
                getUerCode
            } = this
            const uerCode = await getUerCode(this)
            localStorage.clear()
            this.loginLoading = true
            if (uerCode) {
                const res = await this.dingLogin(uerCode)
                this.loginLoading = false
                this.freeLoginTip = ''
                if (res.code !== 200 || !res.success) {
                    message.warning(res.message)
                } else {
                    const avatar = res.data.avatar
                    const roleNow = localStorage.getItem('roleNow')
                    if (!roleNow) {
                        localStorage.setItem('roleNow', res.data.roleIdList[0].children[0].name)
                    }
                    localStorage.setItem('roleIdList', JSON.stringify(res.data.roleIdList[0].children))
                    localStorage.setItem('avatar', avatar)
                    localStorage.setItem('token', res.token)
                    localStorage.setItem('userId', res.data.id)
                    localStorage.setItem('userName', res.data.trueName)
                    const enterpriseId =res.data.enterprise && (res.data.enterprise[0] ? res.data.enterprise[0].code : '');
                    localStorage.setItem('enterpriseId', enterpriseId)
                    if (this.userName || this.userName === '') {
                        this.userName = res.data.trueName
                    }
                    this.pageInit ? this.pageInit() : '';
                }
            } else {
                this.loginLoading = false
                message.warning('调用钉钉userCode失败')
            }
            // { auth: result.code }
        },
        toGoLogin() {
            const token = localStorage.getItem('token')
            const loginType = localStorage.getItem('loginType')
            const {
                ready,
                freeLogin
            } = this
            const {
                name
            } = this.$route
            if (loginType === 'old') {
                return ready(freeLogin)
            }
            if (name !== 'Home') {
                if (token) {
                    this.pageInit()
                    return
                }
            }
            // ready(freeLogin)
            if (token) {
                this.pageInit()
                return
            }
            ready(freeLogin)
        }
    },
    created() {
        this.toGoLogin()
    },
}